package de.unibi.mmi.homeauto.team1.mainview;

import de.unibi.mmi.homeauto.team1.R;
import de.unibi.mmi.homeauto.team1.client.Client;
import de.unibi.mmi.homeauto.team1.constants.BATH;
import de.unibi.mmi.homeauto.team1.constants.BEDROOM;
import de.unibi.mmi.homeauto.team1.constants.CORRIDOR;
import de.unibi.mmi.homeauto.team1.constants.DeviceValues;
import de.unibi.mmi.homeauto.team1.constants.KITCHEN;
import de.unibi.mmi.homeauto.team1.constants.LIVINGROOM;
import de.unibi.mmi.homeauto.team1.devices.DeviceViewPager;
import de.unibi.mmi.homeauto.team1.drafts.ImageFragment;

public class ActiveDevicesFragment extends ImageFragment {

	public ActiveDevicesFragment() {
		super();
		this.numColumns = 3;
		this.widthFrac = 3.5;

		this.symbols = new Symbol[0];
		
		this.backgroundID = R.drawable.home_background;
	}

	@Override
	public String getTitle() {
		return "Aktiv";
	}

	private static int[] LAMP_IDS = { BATH.LAMP_1_ID, BATH.LAMP_2_ID,
			LIVINGROOM.LAMP_1_ID, LIVINGROOM.LAMP_2_ID, CORRIDOR.LAMP_ID,
			KITCHEN.LAMP_1_ID, KITCHEN.LAMP_2_ID, BEDROOM.LAMP_1_ID,
			BEDROOM.LAMP_2_ID };

	private static int[] WINDOW_IDS = { BATH.WINDOW_ID, LIVINGROOM.WINDOW_ID,
			BEDROOM.WINDOW_ID, KITCHEN.WINDOW_ID };

	private static int[] DOOR_IDS = { BATH.DOOR_ID, LIVINGROOM.DOOR_ID,
			BEDROOM.DOOR_ID, KITCHEN.DOOR_ID, CORRIDOR.DOOR_ID };

	private static int[] FIRE_IDS = { BATH.SMOKE_ID, KITCHEN.SMOKE_ID,
			LIVINGROOM.SMOKE_ID, BEDROOM.SMOKE_ID, CORRIDOR.SMOKE_ID };

	@Override
	public void serverValuesUpdated() {
		int lamp = 0;
		int window = 0;
		int door = 0;

		int fire = 0;

		for (int id : FIRE_IDS) {
			if (Client.getValue(id) == -1.0f) {
				fire = R.drawable.fire_broken;
				break;
			}
		}

		int oven = (Client.getValue(KITCHEN.COOKER_ID) + Client
				.getValue(KITCHEN.OVEN_ID)) != 0 ? R.drawable.oven_active : 0;

		int tub = Client.getValue(BATH.CONFLUX_ID) != 0
				|| (Client.getValue(BATH.EFFLUX_ID) == DeviceValues.EFFLUX_CLOSED && Client
						.getValue(BATH.WATER_HEIGHT_ID) != 0) ? R.drawable.tub_active
				: 0;

		for (int id : LAMP_IDS) {
			// Das hier wegen des Fehlerzustands der Lampen, der -1 ist
			lamp += Client.getValue(id) == Float.valueOf(1.0f) ? 1 : 0;
		}
		for (int id : WINDOW_IDS) {
			// Bei gekippt oder auf
			window += (Client.getValue(id) == Float.valueOf(1.0f) || Client
					.getValue(id) == Float.valueOf(2.0f)) ? 1 : 0;
		}
		for (int id : DOOR_IDS) {
			door += Client.getValue(id) == Float.valueOf(1.0f) ? 1 : 0;
		}

		int active = 0;

		for (int i : new int[] { lamp, window, door, oven, tub, fire }) {
			if (i != 0) {
				++active;
			}
		}

		this.symbols = new Symbol[active];

		switch (door) {
		case (1):
			door = R.drawable.door_1;
			break;
		case (2):
			door = R.drawable.door_2;
			break;
		case (3):
			door = R.drawable.door_3;
			break;
		case (4):
			door = R.drawable.door_4;
			break;
		case (5):
			door = R.drawable.door_all;
			break;
		}

		switch (window) {
		case (1):
			window = R.drawable.window_1;
			break;
		case (2):
			window = R.drawable.window_2;
			break;
		case (3):
			window = R.drawable.window_3;
			break;
		case (4):
			window = R.drawable.window_all;
			break;
		}

		switch (lamp) {
		case (1):
			lamp = R.drawable.bulb_1;
			break;
		case (2):
			lamp = R.drawable.bulb_2;
			break;
		case (3):
			lamp = R.drawable.bulb_3;
			break;
		case (4):
			lamp = R.drawable.bulb_4;
			break;
		case (5):
			lamp = R.drawable.bulb_5;
			break;
		case (6):
			lamp = R.drawable.bulb_6;
			break;
		case (7):
			lamp = R.drawable.bulb_7;
			break;
		case (8):
			lamp = R.drawable.bulb_8;
			break;
		case (9):
			lamp = R.drawable.bulb_all;
			break;
		}

		active = 0;

		if (fire != 0) {
			symbols[active++] = new Symbol(fire, MainViewActivity.DEVICES_POS,
					DeviceViewPager.FIRE_POS);
		}

		if (lamp != 0) {
			symbols[active++] = new Symbol(lamp, MainViewActivity.DEVICES_POS,
					DeviceViewPager.LAMP_POS);
		}

		if (window != 0) {
			symbols[active++] = new Symbol(window,
					MainViewActivity.DEVICES_POS, DeviceViewPager.WINDOW_POS);
		}

		if (door != 0) {
			symbols[active++] = new Symbol(door, MainViewActivity.DEVICES_POS,
					DeviceViewPager.DOOR_POS);
		}

		if (oven != 0) {
			symbols[active++] = new Symbol(oven, MainViewActivity.DEVICES_POS,
					DeviceViewPager.OVEN_POS);
		}

		if (tub != 0) {
			symbols[active++] = new Symbol(tub, MainViewActivity.DEVICES_POS,
					DeviceViewPager.TUB_POS);
		}

		this.updateView();
	}
}